<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/20
 * Time: 11:31
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Banner extends Base
{
    /**
     * 轮播图首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $where = [];
            if(isset($keyword) && $keyword != ''){
                $where['banner_title'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($banner_type) && $banner_type != "")
            {
                $where['banner_type'] = $banner_type;
            }
            if(isset($banner_type_id) && $banner_type_id != "")
            {
                $where['banner_type_id'] = $banner_type_id;
            }
            if(isset($banner_fid) && $banner_fid != "")
            {
                $where['banner_fid'] = $banner_fid;
            }
            if(isset($banner_status) && $banner_status != "")
            {
                $where['banner_status'] = $banner_status;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('banner')->where($where)->count();// 获取总条数
            $lists = Db::name('banner')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('banner_status asc,banner_sort desc,banner_update_time desc')
                ->field('banner_content',true)
                ->select();
            if($lists){
                foreach ($lists as $k => $v){
                    $lists[$k]['typename'] = '--';
                    if($v['banner_type'] == 1){
                        $lists[$k]['typename'] = Db::table('up_news_type')->where('nt_id',$v['banner_type_id'])->value('nt_name');
                    }
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $types = Db::name('news_type')
            ->field('nt_id,nt_name,nt_fid')
            ->order('nt_fid asc,nt_sort desc')
            ->select();
        $this->assign('types',$types);
        return $this->fetch();
    }
    
    /**
     * 添加banner
     * @return mixed|\think\response\Json
     */
    public function bannerAdd(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'banner_type' => $banner_type,
                'banner_title' => $banner_title,
                'banner_img' => $banner_img,
                'banner_sort' => $banner_sort,
                'banner_status' => $banner_status,
                'banner_content' => $banner_content,
                'banner_time' => date('Y-m-d H:i:s'),
                'banner_update_time' => date('Y-m-d H:i:s')
            ];
            if($banner_type == 4){
                $data['banner_fid'] = '';
            }else{
                if(!$banner_fid){
                    return json(['code' => 100, 'msg' => '请选择类型']);
                }
                $data['banner_fid'] = $banner_fid;
                if($banner_type == 1 || $banner_type == 2){
                    if($banner_fid == 1){
                        if(!$banner_type_id1){
                            return json(['code' => 100, 'msg' => '请选择分类']);
                        }
                        $data['banner_type_id'] = $banner_type_id1;
                    }else{
                        if(!$banner_type_id2){
                            return json(['code' => 100, 'msg' => '请选择分类']);
                        }
                        $data['banner_type_id'] = $banner_type_id2;
                    }
                }
            }
            $res = Db::table('up_banner')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $types1 = Db::name('news_type')
            ->where('nt_fid',1)
            ->field('nt_id,nt_name,nt_fid')
            ->order('nt_sort desc')
            ->select();
        $types2 = Db::name('news_type')
            ->where('nt_fid',2)
            ->field('nt_id,nt_name,nt_fid')
            ->order('nt_sort desc')
            ->select();
        $this->assign([
            'types1' => $types1,
            'types2' => $types2,
        ]);
        return $this->fetch('banner/banneradd');
    }
    
    /**
     * 编辑轮播图
     * @return mixed|\think\response\Json
     */
    public function bannerEdit(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'banner_title' => $banner_title,
                'banner_img' => $banner_img,
                'banner_content' => $banner_content,
                'banner_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_banner')->where('banner_id', $banner_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $banner_id = input('banner_id');
        // banner
        $banner = Db::table('up_banner')
            ->where('banner_id', $banner_id)
            ->find();
        $this->assign([
            'banner' => $banner
        ]);
        return $this->fetch('banner/banneredit');
    }
    
    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        $id = input('id');
        $param = [
            'banner_sort'=>input('value'),
        ];
        $res = Db::name('banner')->where('banner_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    
    /**
     * 上架
     * @return \think\response\Json
     */
    public function bannerOn(){
        $banner_id = input('id');
        $res = Db::table('up_banner')->where('banner_id', $banner_id)->setField('banner_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '上架成功']);
        }else{
            return json(['code' => 100, 'msg' => '上架失败']);
        }
    }
    
    /**
     * 下架
     * @return \think\response\Json
     */
    public function bannerDown(){
        $banner_id = input('id');
        $res = Db::table('up_banner')->where('banner_id', $banner_id)->setField('banner_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '下架成功']);
        }else{
            return json(['code' => 100, 'msg' => '下架失败']);
        }
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function bannerDel(){
        $banner_id = input('id');
        $res = Db::table('up_banner')->where('banner_id', $banner_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}